package com.um.jdy.common.commons.configuration;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import com.um.springboot.starter.properties.HshProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

/**
 * 初始化sa-token
 *
 * @author 甘天伟
 * @date 2025/3/7
 * 版权所有 广州优匠科技有限公司
 */
@Configuration
@ConditionalOnProperty(value = "hsh.config.session-type", havingValue = "sa-token")
public class SaTokenConfig implements WebMvcConfigurer {

    @Autowired
    HshProperties hshProperties;

    @PostConstruct
    public void init() {
        // 设置默认StpLogic当前服务用户表，避免服务之间共用token引发安全问题
        StpUtil.setStpLogic(new StpLogic(hshProperties.getToken().getUserTableName()));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加sa-token拦截器
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
    }

}
